package com.ruoyi.monitor.infoprojectmonitorrelation.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.monitor.infoprojectmonitor.domain.InfoProjectMonitor;
import com.ruoyi.monitor.infoprojectmonitor.mapper.InfoProjectMonitorMapper;
import com.ruoyi.monitor.infoprojectmonitorfunds.domain.InfoProjectMonitorFunds;
import com.ruoyi.monitor.infoprojectmonitorindex.domain.InfoProjectMonitorIndex;
import com.ruoyi.monitor.infoprojectmonitorrelation.vo.InfoProjectMonitorDetailVo;
import com.ruoyi.monitor.infoprojectmonitorrelation.vo.InfoProjectMonitorExamineVo;
import com.ruoyi.monitor.infoprojectmonitorfunds.mapper.InfoProjectMonitorFundsMapper;
import com.ruoyi.monitor.infoprojectmonitorindex.mapper.InfoProjectMonitorIndexMapper;
import com.ruoyi.monitor.infoprojectmonitorrelation.domain.InfoProjectMonitorRelation;
import com.ruoyi.monitor.infoprojectmonitorrelation.mapper.InfoProjectMonitorRelationMapper;
import com.ruoyi.monitor.infoprojectmonitorrelation.service.IInfoProjectMonitorRelationService;
import com.ruoyi.monitor.infoprojectmonitorrelation.vo.InfoProjectMonitorReportVo;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * 项目监控 关联资金和指标 中间Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-16
 */
@Service
public class InfoProjectMonitorRelationServiceImpl implements IInfoProjectMonitorRelationService {
    @Resource
    private InfoProjectMonitorRelationMapper infoProjectMonitorRelationMapper;
    @Resource
    private InfoProjectMonitorFundsMapper infoProjectMonitorFundsMapper;
    @Resource
    private InfoProjectMonitorIndexMapper infoProjectMonitorIndexMapper;
    @Resource
    private SysDictDataMapper sysDictDataMapper;
    @Resource
    private InfoProjectMonitorMapper infoProjectMonitorMapper;


    /**
     * 查询项目监控 关联资金和指标 中间
     *
     * @param id 项目监控 关联资金和指标 中间主键
     * @return 项目监控 关联资金和指标 中间
     */
    @Override
    public InfoProjectMonitorRelation selectInfoProjectMonitorRelationById(Long id) {
        return infoProjectMonitorRelationMapper.selectInfoProjectMonitorRelationById(id);
    }

    /**
     * 查询项目监控 关联资金和指标 中间列表
     *
     * @param infoProjectMonitorRelation 项目监控 关联资金和指标 中间
     * @return 项目监控 关联资金和指标 中间
     */
    @Override
    public List<InfoProjectMonitorRelation> selectInfoProjectMonitorRelationList(InfoProjectMonitorRelation infoProjectMonitorRelation) {
        return infoProjectMonitorRelationMapper.selectInfoProjectMonitorRelationList(infoProjectMonitorRelation);
    }

    /**
     * 新增项目监控 关联资金和指标 中间
     *
     * @param infoProjectMonitorRelation 项目监控 关联资金和指标 中间
     * @return 结果
     */
    @Override
    public int insertInfoProjectMonitorRelation(InfoProjectMonitorRelation infoProjectMonitorRelation) {
        return infoProjectMonitorRelationMapper.insertInfoProjectMonitorRelation(infoProjectMonitorRelation);
    }

    /**
     * 修改项目监控 关联资金和指标 中间
     *
     * @param infoProjectMonitorRelation 项目监控 关联资金和指标 中间
     * @return 结果
     */
    @Override
    public int updateInfoProjectMonitorRelation(InfoProjectMonitorRelation infoProjectMonitorRelation) {
        infoProjectMonitorRelation.setUpdateTime(DateUtils.getNowDate());
        return infoProjectMonitorRelationMapper.updateInfoProjectMonitorRelation(infoProjectMonitorRelation);
    }

    /**
     * 批量删除项目监控 关联资金和指标 中间
     *
     * @param ids 需要删除的项目监控 关联资金和指标 中间主键
     * @return 结果
     */
    @Override
    public int deleteInfoProjectMonitorRelationByIds(Long[] ids) {
        return infoProjectMonitorRelationMapper.deleteInfoProjectMonitorRelationByIds(ids);
    }

    /**
     * 删除项目监控 关联资金和指标 中间信息
     *
     * @param id 项目监控 关联资金和指标 中间主键
     * @return 结果
     */
    @Override
    public int deleteInfoProjectMonitorRelationById(Long id) {
        return infoProjectMonitorRelationMapper.deleteInfoProjectMonitorRelationById(id);
    }

    @Transactional
    @Override
    public int deleteProjectMonitorRelation(Long id) {
        //1.删除主表
        int i = infoProjectMonitorRelationMapper.deleteInfoProjectMonitorRelationById(id);
        //2.删除监控资金
        int i1 = infoProjectMonitorFundsMapper.deleteMonitorFundsByRelationId(id);
        //3.删除监控指标
        int i2 = infoProjectMonitorIndexMapper.deleteMonitorIndexByRelationId(id);
        if (i < 0 || i1 < 0 || i2 < 0) {
            //有删除失败
            throw new RuntimeException("撤回失败");
        }
        return 1;
    }

    @Override
    public List<InfoProjectMonitorRelation> selectProjectMonitorRelationListByProjectId(Long monitorProjectId) {
        return infoProjectMonitorRelationMapper.selectProjectMonitorRelationListByProjectId(monitorProjectId);
    }

    @Override
    public List<InfoProjectMonitorReportVo> reportProjectMonitorList(InfoProjectMonitorReportVo monitorReportVo) {
        return infoProjectMonitorRelationMapper.reportProjectMonitorList(monitorReportVo);
    }

    @Transactional
    @Override
    public int examineProjectMonitor(InfoProjectMonitorExamineVo infoProjectMonitorExamineVo) {
        //修改项目监控关系表中的审核状态
        InfoProjectMonitorRelation monitorRelation = new InfoProjectMonitorRelation();
        monitorRelation.setId(infoProjectMonitorExamineVo.getMonitorRelationId());
        monitorRelation.setMonitorStatus(infoProjectMonitorExamineVo.getExamineStatus());
        monitorRelation.setMonitorRemarks(infoProjectMonitorExamineVo.getExamineRemarks());
        monitorRelation.setSjcRemarks(infoProjectMonitorExamineVo.getSjcRemarks());
        int i = infoProjectMonitorRelationMapper.updateInfoProjectMonitorRelation(monitorRelation);
        return i;
    }

    @Transactional
    @Override
    public int saveProjectMonitorItem(InfoProjectMonitorFunds infoProjectMonitorFunds) {
        //1.修改资金的使用情况以及（下达时间，下达数量）
        infoProjectMonitorFunds.setUpdateTime(new Date());
        infoProjectMonitorFunds.setUpdateBy(SecurityUtils.getLoginUser().getUser().getDept().getDeptName() + "-" + SecurityUtils.getLoginUser().getUser().getNickName());
        infoProjectMonitorFundsMapper.updateInfoProjectMonitorFunds(infoProjectMonitorFunds);
        //2.获取关系表的这条记录
        //关系表主键id
        Long projectMonitorRelationId = infoProjectMonitorFunds.getProjectMonitorRelationId();
        InfoProjectMonitorRelation infoProjectMonitorRelation = infoProjectMonitorRelationMapper.selectInfoProjectMonitorRelationById(projectMonitorRelationId);
        //1.1 将资金执行率更新进关系表中
        infoProjectMonitorRelation.setCapitalRate(infoProjectMonitorFunds.getTotalFundsExecuteRate());
        //1.2 查看指标中是否含  可以完成(1) 部分完成(2) 不可能完成(3)
        //如果含有 不可能完成的 外面就显示不可能完成   含有部分完成的 就显示部分完成
        //获取数据

        //不可能完成的数量
        List<Integer> finishIntegers = infoProjectMonitorIndexMapper.selectFinishIntegers(projectMonitorRelationId);
        //检查是否含有空巷
        for (Integer finishInteger : finishIntegers) {
            if (finishInteger == null) {
                throw new RuntimeException("保存失败,请检查指标填写");
            }
        }
        //顺序不要改变 1 2 3
        infoProjectMonitorRelation.setIndexFinishStatus(1);
        if (finishIntegers.contains(2)) {
            infoProjectMonitorRelation.setIndexFinishStatus(2);
        }
        if (finishIntegers.contains(3)) {
            infoProjectMonitorRelation.setIndexFinishStatus(3);
        }
        //修改关系表
        int i = infoProjectMonitorRelationMapper.updateInfoProjectMonitorRelation(infoProjectMonitorRelation);
        return i;
    }

    @Override
    public InfoProjectMonitorDetailVo getProjectMonitorDetail(Long relationMonitorId) {
        //1.查询项目信息
        InfoProjectMonitorDetailVo detailVo = infoProjectMonitorRelationMapper.getProjectMonitorDetail(relationMonitorId);
        if (null == detailVo) {
            throw new RuntimeException("项目不存在");
        }
        //2.查询该期的资金情况
        InfoProjectMonitorFunds infoProjectMonitorFunds = new InfoProjectMonitorFunds();
        infoProjectMonitorFunds.setProjectMonitorRelationId(relationMonitorId);
        List<InfoProjectMonitorFunds> infoProjectMonitorFunds1 = infoProjectMonitorFundsMapper.selectInfoProjectMonitorFundsList(infoProjectMonitorFunds);
        if (!infoProjectMonitorFunds1.isEmpty()) {
            detailVo.setInfoProjectMonitorFunds(infoProjectMonitorFunds1.get(0));
        }
        //3.查询该执行期的指标数据
        InfoProjectMonitorIndex infoProjectMonitorIndex = new InfoProjectMonitorIndex();
        infoProjectMonitorIndex.setProjectMonitorRelationId(relationMonitorId);
        List<InfoProjectMonitorIndex> infoProjectMonitorIndices = infoProjectMonitorIndexMapper.selectInfoProjectMonitorIndexList(infoProjectMonitorIndex);

        detailVo.setInfoProjectMonitorIndexList(infoProjectMonitorIndices);

        return detailVo;
    }
}
